﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ChanTaiApi
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void initialBt_Click(object sender, EventArgs e)
        {
            using (var ui = new AutoDisable(initialBt))
            {
                this.Cursor = Cursors.WaitCursor;
                if (initialBt.Text.IndexOf("安装", StringComparison.Ordinal) >= 0)
                {
                    try
                    {
                        if (WindowsServiceManager.IsServiceExisted(Service.Name))
                        {
                            WindowsServiceManager.UnInstallService(Service.Name);
                        }

                        WindowsServiceManager.InstallService(Service.Name);
                        MessageBox.Show("安装成功");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                else
                {
                    try
                    {
                        if (WindowsServiceManager.IsServiceExisted(Service.Name))
                        {
                            WindowsServiceManager.UnInstallService(Service.Name);
                        }
                        MessageBox.Show("卸载成功");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                this.Cursor = Cursors.Arrow;
            }
        }

        private void startBt_Click(object sender, EventArgs e)
        {
            using (var ui = new AutoDisable(startBt))
            {
                this.Cursor = Cursors.WaitCursor;
                if (startBt.Text.IndexOf("启动") >= 0)
                {
                    StopAndStartService();
                }
                else
                {
                    WindowsServiceManager.StopService(Service.Name);
                }

                this.Cursor = Cursors.Arrow;
            }
        }
        private void StopAndStartService()
        {
            if (WindowsServiceManager.IsServiceStarted(Service.Name))
            {
                WindowsServiceManager.StopService(Service.Name);
            }

            WindowsServiceManager.StartService(Service.Name);
        }

        private void RefreshStatus()
        {
            try
            {
                lblServiceStatus.Text = "";

                if (!WindowsServiceManager.IsServiceExisted(Service.Name))
                {
                    lblServiceStatus.Text = "服务未安装";
                    lblServiceStatus.ForeColor = Color.Red;
                    initialBt.Text = "安装服务(&I)";
                    return;
                }

                initialBt.Text = "卸载服务(&U)";

                if (!WindowsServiceManager.IsServiceStarted(Service.Name))
                {
                    lblServiceStatus.Text = "服务已停止";
                    lblServiceStatus.ForeColor = Color.Red;
                    startBt.Text = "启动服务(&S)";
                    return;
                }

                lblServiceStatus.Text = "服务已启动";
                lblServiceStatus.ForeColor = Color.DarkGreen;
                startBt.Text = "停止服务(&S)";

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void closeBt_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            RefreshStatus();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            RefreshStatus();
        }
    }
}
